﻿Public Class NOTIFYMESSAGE
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_MASNOTIFYMESSAGE As New PROVIDERSCLASS

    Private Sub NOTIFYMESSAGE_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridtitle()

        DesignForm(Me)
        DesignGrid(MASTITLEGrid)

        PROVIDER_MASNOTIFYMESSAGE.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridtitle()
        Dim objSelectDatamastitle As New mastitle
        Dim MASTITLE As DataTable
        MASTITLE = connect.GetTable(objSelectDatamastitle.selectMastitle_)
        MASTITLEGrid.PrimaryGrid.DataSource = MASTITLE
    End Sub

    Private Sub MASTITLEGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASTITLEGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสเรื่อง").Value() IsNot DBNull.Value Then
            TITLEIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสเรื่อง").Value()
        Else
            TITLEIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อเรื่อง").Value() IsNot DBNull.Value Then
            TITLETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ชื่อเรื่อง").Value()
        Else
            TITLETxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value() = True Then
            titlestatusCheckbox.Checked = True
        Else
            titlestatusCheckbox.Checked = False
        End If
        connect = CONDB.NewConnection()
        If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
            Try
                Dim Sql As String
                connect.BeginTrans()
                Sql = "UPDATE mastitle SET "
                If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
                    Sql += "`status` = 1 "
                Else
                    Sql += "`status` = 0 "
                End If
                Sql += "WHERE `id` = '" & TITLEIDTxt.Text & "' ;"
                connect.ExecuteNonQuery(Sql)
                connect.CommitTrans()
            Catch ex As Exception
                connect.RollbackTrans()
                MsgBox(ex.ToString)
            End Try
        End If
        connect.Dispose()
        'connectlocal = conlocal.NewConnection()
        'If e.GridCell.GridColumn.Name = "สถานะการเปิดใช้งาน" Then
        '    Try
        '        Dim Sql As String
        '        connectlocal.BeginTrans()
        '        Sql = "UPDATE MASTITLE SET "
        '        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("สถานะการเปิดใช้งาน").Value()) = True Then
        '            Sql += "[STATUS] = 1 "
        '        Else
        '            Sql += "[STATUS] = 0 "
        '        End If
        '        Sql += "WHERE [ID] = '" & TITLEIDTxt.Text & "' ;"

        '        connectlocal.ExecuteNonQuery(Sql)
        '        connectlocal.CommitTrans()
        '    Catch ex As Exception
        '        connectlocal.RollbackTrans()
        '        MsgBox(ex.ToString)
        '    End Try
        'End If
        'connectlocal.Dispose()
        addTitle.Enabled = False
    End Sub

    Private Sub addTitle_Click(sender As Object, e As EventArgs) Handles addTitle.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASNOTIFYMESSAGE.PADD_ = True Then
                Dim objInsertData As New mastitle
                objInsertData.titleTitle_ = TITLETxt.Text
                objInsertData.titleStatus_ = titlestatusCheckbox.Checked
                objInsertData.insertMastitleData()

                SelectDatagridtitle()
            Else
                MsgBox(PROVIDER_MASNOTIFYMESSAGE.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editTitle_Click(sender As Object, e As EventArgs) Handles editTitle.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASNOTIFYMESSAGE.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลการแจ้งเตือนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                If editStat = 1 Then
                    Dim objEditData As New mastitle
                    objEditData.titleID_ = TITLEIDTxt.Text
                    objEditData.titleTitle_ = TITLETxt.Text
                    objEditData.titleStatus_ = titlestatusCheckbox.Checked
                    objEditData.editMastitleData()

                    SelectDatagridtitle()

                End If
                addTitle.Enabled = True
            Else
                MsgBox(PROVIDER_MASNOTIFYMESSAGE.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(mastitle.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(mastitle.Text)
        nextform.ShowDialog()
    End Sub
    
End Class